#include "page_base.h"
#include "wind_mgr.h"

PageBase::PageBase(ViewGroup *dPageBox, const std::string resource, int pageType)
    : dPageBox(dPageBox), dPageType(pageType)
{

    dPageLayout = nullptr;
    dTitleLeftClock = nullptr;
    dTitleLeftWifi = nullptr;
    dTitleLeftSock = nullptr;
    dTitleLeftLight = nullptr;
    dTitleLeftSetting = nullptr;

    initPageBase(resource);
}

PageBase::~PageBase()
{
    LOGE("PageBase::~PageBase  this = %p", this);
    delete dPageLayout;
}

void PageBase::initPageBase(const std::string resource)
{

    dPageLayout = (ViewGroup *)LayoutInflater::from(dPageBox->getContext())->inflate(resource, nullptr);
    addPage();
    if (dPageLayout != nullptr)
    {
        dTitleLeftClock = (TimeTextView *)dPageLayout->findViewById(demo::R::id::left_text);
        // dTitleLeftClock = (TextView *)dPageLayout->findViewById(demo::R::id::left_text);
        dTitleLeftWifi = (ImageView *)dPageLayout->findViewById(demo::R::id::left_image1);
        dTitleLeftSock = (ImageView *)dPageLayout->findViewById(demo::R::id::left_image2);
        dTitleLeftLight = (ImageView *)dPageLayout->findViewById(demo::R::id::left_image3);
        dTitleLeftSetting = (ImageView *)dPageLayout->findViewById(demo::R::id::left_image4);

        dTitleLeftSetting->setOnClickListener([this](View &)
                                              { d_windMgr->showPage(PAGE_SETTING); });
    }
    dTitleLeftClock->start();

    myTickerTime = 1000;
    myTicker = std::bind(&PageBase::onTicker, this);
    onTicker();

    // dTitleLeftClock->setOnClickListener([this](View &)
    // { g_tuYaObjConnMgr->send2MCU(TUYA_WIFI_CMD_REQUEST_TIME); });
}

void PageBase::onTicker()
{
    dTitleLeftClock->setText(getTimeStr());
    dPageLayout->postDelayed(myTicker, myTickerTime);
}

void PageBase::addPage()
{
    // LOGE("----------------------------------addPage  this = %p  id = %d dPageType = %d",this,dPageLayout->getId(),dPageType);
    dPageBox->addView(dPageLayout);
}

void PageBase::removePage()
{
    // LOGE("----------------------------------removePage  this = %p  id = %d dPageType = %d",this,dPageLayout->getId(),dPageType);
    dPageLayout->removeCallbacks(myTicker);
    dTitleLeftClock->stop();
    dPageBox->removeView(dPageLayout);
}

void PageBase::startPageAction(pageType type, int position) {

};

void PageBase::stopPageAction()
{
    dPageLayout->removeCallbacks(myTicker);
}

void PageBase::callMsg(int type, void *data)
{
    onMsg(type, data);
}

void PageBase::onMsg(int type, void *data)
{
}